package com.fastloan.jn.mgt.all.table.controller;

import com.fastloan.jn.mgt.all.table.exceptions.TableReaderException;
import com.fastloan.jn.mgt.all.table.model.*;
import com.fastloan.jn.mgt.all.table.service.AllTableService;
import com.fastloan.jn.mgt.application.basic.model.JnApplication;
import com.fastloan.jn.mgt.application.basic.service.JnApplicationService;
import com.fastloan.jn.utils.other.ObjUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.HtmlUtils;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by gelon on 2017/11/1.
 */
@RestController
@RequestMapping("/api/web/table")
public class TableController extends BaseController<AllTable>{

    @Autowired
    AllTableService tableService;

    @Autowired
    JnApplicationService applicationService ;


    @PostMapping("/upload")
    public ResultTo readerExcel(MultipartFile file,@NotNull String relevanceId) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
            InputStream is = getExcelInput(file);
            if(null == is) { return new ResultTo(ResultEnum.PARAM_ERROR);}
            return new ResultTo().setData(tableService.saveUnique(is,file.getOriginalFilename(),relevanceId));
    }


    public InputStream getExcelInput(MultipartFile file) {
        if (file == null) return null;
        String filename = file.getOriginalFilename();
        long size = file.getSize();
        if (ObjUtil.isEmpty(filename) || size == 0) return null;
        if (filename.endsWith("xlsx")) {
            try {
                return file.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        return null ;
    }


    @PostMapping("/tables")
    public ResultTo getTables(@NotNull String relevanceId) {
        List<AllTable> allTables = tableService.getTable(relevanceId);
        if(allTables == null ) {return new ResultTo(ResultEnum.PARAM_ERROR);}
        return new ResultTo().setData(allTables);
    }


    @PostMapping("/tableValues")
    public ResultTo tableValues(String tableValueJson,@NotNull String relevanceId){
        tableValueJson = HtmlUtils.htmlUnescape(tableValueJson);
        Gson gson = new Gson();
        Type type = new TypeToken<List<AllCellValue>>(){}.getType();
        List<AllCellValue> allCellValues = gson.fromJson(tableValueJson,type);
        tableService.save(allCellValues,relevanceId);
        return new ResultTo();
    }


    @PostMapping("/appTables")
    public ResultTo appTables(MultipartFile file,@NotNull String relevanceId,String appId) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException, IOException {
        InputStream is = getExcelInput(file);
        if(null == is) { return new ResultTo(ResultEnum.PARAM_ERROR);}
        try{
            tableService.saveAppTables(is,relevanceId,file.getOriginalFilename(),appId);
        } catch (TableReaderException e){
            return new ResultTo(ResultEnum.OPERATION_FAILED,e.getMessage());
        } catch (Exception e){
            return new ResultTo(ResultEnum.OPERATION_FAILED);
        }
        return new ResultTo();
    }










}
